package components.plugins.effects
{
	import UIFree.ObjectEvent;
	import UIFree.abox.uif_moduleBox;
	import UIFree.controls.uif_popupMenu;
	import UIFree.uif_component;
	
	import UIUtils.uif_json;
	
	import classes.clt_events;
	
	import components.clt_arrowB;
	import components.clt_component;
	import components.clt_popupMenu;
	import components.clt_selectBase;
	import components.tools.clt_selectLayer;
	import components.tools.clt_toolsBlock;
	
	import data.cd_background;
	import data.effects.cd_effects;
	
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.geom.Rectangle;
	
	public class clt_effectLayer extends clt_selectLayer {
		private var _effects		: Object;
		private var _curEffectIndex	: int;
		private var _curEffect		: clt_baseEffect;
		private var _applyButton	: clt_arrowB;
		private var _deleteButton	: clt_arrowB;
		private var _popupMenu		: clt_popupMenu;
		
		public static var TEXTURESPATH			: String = 'textures/big/';
		public static var TEXTURESPATHPREVIEW	: String = 'textures/preview/';
		public static var EFFECTSELECT			: String = 'EFFECTSELECT';
		
		public function clt_effectLayer(a_clt: clt_clothingBase) {
			var rect: Rectangle = a_clt.tools.toolLayer.getInner(); 
			super(a_clt.tools.toolLayer, cd_effects, clt_servers.DATA_URL + TEXTURESPATHPREVIEW,
				EFFECTSELECT, rect.x, rect.y, rect.width - 30, rect.height);
			visible = false;
			rect = getInner();
			rect.top 	= rect.bottom - 35;
			rect.width  = 230;
			
			_deleteButton = addButton(clt.classes.locale.REMOVEEFFECT, doRemoveEffect, rect);
			_deleteButton.setVisibility(false);
			
			rect 		= getInner();
			rect.top 	= rect.bottom - 35;
			rect.left 	= rect.width - 230; 
			_applyButton = addButton(clt.classes.locale.REMOVEEFFECT, doApplyEffect, rect, 'saveB');
			_applyButton.setVisibility(false);
			_effects = new Object();
			
			_selectComponent.tabs.addEvent(Event.CHANGE, function(e: Event): void {
				var index: int = _selectComponent.tabs.selectItem.object.id;
				setCurEffect(index, null);
				_deleteButton.slideVisible(_effects[index] != null);
			});
			
			_popupMenu = new clt_popupMenu(clt.topLayer, null, [
				{
					caption: 'Настройки'
				}
			], doPopupMenu, {
				orientation	: uif_popupMenu.RIGHTPOSITION
			});
		}
		
		protected function doPopupMenu(e: ObjectEvent): void {
			workspace.dispatchEvent(new ObjectEvent(clt_events.EFFECTPARAMS, _curEffect));
		}
		
		protected function doRemoveEffect(e: Event): void {
			if (_curEffectIndex && _effects[_curEffectIndex]) {
				delete(_effects[_curEffectIndex]);
				setCurGroup(_curEffectIndex);
			}
		}
		
		protected function doApplyEffect(e: Event): void {
			applyCurEffect();
		}
		
		protected function applyCurEffect(): void {
			if (_curEffectIndex && _curEffect) {
				clt.checkVipStatus(function(): void {
					_effects[_curEffectIndex] = _curEffect;
					setCurEffect(_curEffectIndex, null);
					setCurGroup(_curEffectIndex);
				});
			}
		}
		
		protected function get isNoApplyEffect(): Boolean {
			return _curEffect != null;
		}
		
		override protected function getSelectRect(): Rectangle {
			var rect: Rectangle = super.getSelectRect();
			rect.bottom -= 40;
			return rect;
		}
		
		public function execute(a_params: Object): void {
			clt.workspace.dispatchEvent(new ObjectEvent(clt_events.SHOWTOOLS, this));
		}
		
		override protected function listenEvents():void {
			super.listenEvents();
			addEvent(EFFECTSELECT, doEffectSelect, clt.workspace); 
			addEvent(clt_events.AFTERDRAWRESULT, doAfterDraw, clt.workspace);
			addEvent(clt_events.GETRESULTBMP, doGetResult, clt.workspace);
			addEvent(KeyboardEvent.KEY_DOWN, doKeyDown, stage);
			addEvent(Event.CHANGE, doChangeState, toolsBlock);
			addEvent(clt_events.CANHIDETOOLS, doCanHide, workspace);
		}
		
		protected function get toolsBlock(): clt_toolsBlock {
			return parent.parent.parent as clt_toolsBlock;
		}
		
		protected function doGetResult(e: ObjectEvent): void {
			if (isNoApplyEffect) {
				if (!e.object.options || !e.object.options.resultOnly) {
					e.object.responseFunc = null;
					clt.alert(clt.locale.WARNING, clt.locale.NOAPPLYEFFECT);
					e.stopPropagation();
				}
			}
		} 
		
		private function doCanHide(e: ObjectEvent): void {
			if (isNoApplyEffect) {
				clt.alert(clt.locale.WARNING, clt.locale.ALTERAPPLYEFFECT, function(): void {
					toolsBlock.closePanel();
				}, true);
				e.object.result = false;
			}
		}
		
		protected function doChangeState(e: Event): void {
			if ((e.target as clt_toolsBlock).showState == 0) {
/*				if (isNoApplyEffect) {
					clt.alert(clt.locale.WARNING, clt.locale.NOAPPLYEFFECT, function(): void {
					});
				}*/
				setCurEffect(0, null);
			}
		}
		
		protected function doKeyDown(e: KeyboardEvent): void {
			if (_curEffectIndex && _curEffect) {
				sysTrace(e.keyCode);
				switch (e.keyCode) {
					case 81: _curEffect.incValue1();
							break;
					case 87: _curEffect.incValue2();
						break;
					case 69: _curEffect.incValue3();
						break;
					case 82: _curEffect.incValue4();
						break;
					case 13: if (e.ctrlKey) {
							clt.alert(clt.classes.locale.MESSAGE, uif_json.e(_effects[_curEffectIndex].params));
						}
						break;
					default: return;
						break;
				}
				clt.workspace.dispatchEvent(new Event(clt_events.DISPLAYREFRESH));
			}
		}
		
		protected function _setCurEffect(a_curEffectIndex: int, a_filter: Object = null): void {
			_curEffectIndex = a_curEffectIndex
			if (_curEffectIndex > 0) {
				_applyButton.slideVisible(a_filter != null);
				if (a_filter) {
					//_selectEffect = a_filter;
					_applyButton.text = clt.classes.locale.REMOVEEFFECT + ' "' + _selectComponent.tabs.selectItem.object.name + '"';
					if (_effects[_curEffectIndex] == a_filter)
						return;
					_effects[_curEffectIndex] = a_filter;
				} else  if (_effects[_curEffectIndex]) {
					delete(_effects[_curEffectIndex]);
					_selectComponent.itemsLayer.selectIndex = -1;
				} else return;
			} else {
				_applyButton.slideVisible(false);
			}
			clt.workspace.dispatchEvent(new Event(clt_events.DISPLAYREFRESH));
		}
		
		protected function setCurEffect(a_curEffectIndex: int, a_filter: clt_baseEffect = null): void {
			_curEffectIndex = a_curEffectIndex;
			if (_curEffectIndex > 0) {
				_applyButton.slideVisible(a_filter != null);
				if (a_filter) {
					//_selectEffect = a_filter;
					_applyButton.text = clt.classes.locale.APPLYEEFFECT + ' "' + _selectComponent.tabs.selectItem.object.name + '"';
					if (_effects[_curEffectIndex] == a_filter)
						return;
					_curEffect = a_filter;
				} else if (_curEffect) {
					_selectComponent.itemsLayer.selectIndex = -1;
					_curEffect = null;
				} else return;
			} else {
				_applyButton.slideVisible(false);
				_selectComponent.itemsLayer.selectIndex = -1;
				_curEffect = null;
			}
			_popupMenu.activeControl = (_curEffect != null)?_selectComponent.itemsLayer.selectItem.control:null;
			clt.workspace.dispatchEvent(new Event(clt_events.DISPLAYREFRESH));
		}
		
		protected function setCurGroup(group: int): void {
			_curEffectIndex = group;
			_deleteButton.slideVisible(_effects[_curEffectIndex] != null);
			clt.workspace.dispatchEvent(new Event(clt_events.DISPLAYREFRESH));
		}
		
		protected function doEffectSelect(e: ObjectEvent): void {
			var aClass: Class = e.object.effectClass?e.object.effectClass:_selectComponent.tabs.selectItem.object.effectClass;
			setCurEffect(e.object.group, new aClass(e.object));
		}
		
		protected function doAfterDraw(e: ObjectEvent): void {
			var bmd: BitmapData = e.object as BitmapData;
			for (var i: int=0; i<cd_effects.groups.length; i++) {
				var id: int = cd_effects.groups[i].id;
				if ((int(id) == _curEffectIndex) && _curEffect) {
					if (e.object == clt.resultLayer.display.bitmapData)
						_curEffect.execute(bmd)
				} else if (_effects[id]) (_effects[id] as clt_baseEffect).execute(bmd); 
			}
		}
	}
}